﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using System.Text;

namespace Web
{
    /// <summary>
    /// 异常等级列举
    /// </summary>
    public enum Level
    {
        /// <summary>
        /// 高级
        /// </summary>
        High,
        /// <summary>
        /// 中级
        /// </summary>
        Middle,
        /// <summary>
        /// 低级
        /// </summary>
        Low,
    }
    /// <summary>
    /// 作者：张占岭
    /// 统一异常过滤器
    /// </summary>
    public class VLogAttribute : FilterAttribute, IExceptionFilter
    {
        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="filterContext"></param>
        private void writeLog(ExceptionContext filterContext)
        {
            StringBuilder err = new StringBuilder();
            Exception objErr = filterContext.Exception.GetBaseException();
            err.Append("Error Caught in Application_Error event");
            err.Append("<br>网站地址：" + HttpContext.Current.Request.Url.ToString());
            err.Append("<br>异常信息：" + objErr.Message.ToString());
            err.Append("<br>栈跟踪：" + objErr.StackTrace.ToString());
            VLog.VLogFactory.CreateVLog().ErrorLog(err.ToString());
            filterContext.RequestContext.HttpContext.Response.Redirect("/Error/Index");
        }
        public Level _level { get; private set; }

        public VLogAttribute(Level level)
        {
            this._level = level;
        }


        #region IExceptionFilter 成员

        public void OnException(ExceptionContext filterContext)
        {
            bool flag = false;//为false,不记录异常
            switch (_level)
            {
                case Level.High:
                    flag = true;
                    break;
                case Level.Middle:
                    flag = flag
                        || filterContext.Exception is DivideByZeroException
                        || filterContext.Exception is NullReferenceException
                        || filterContext.Exception is HttpException
                        || filterContext.Exception is FormatException
                        || filterContext.Exception is ArgumentException
                        || filterContext.Exception is ArgumentNullException
                        || filterContext.Exception is ArgumentOutOfRangeException
                        || filterContext.Exception is NullReferenceException;
                    break;
                case Level.Low:
                    break;
                default:
                    break;
            }
            if (!filterContext.ExceptionHandled && flag)
            {
                {
                    filterContext.ExceptionHandled = true;
                    writeLog(filterContext);
                }
            }
        }

        #endregion
    }
}
